from pathlib import PurePath
from typing import Sequence

from common.types import AgentID
from infection_monkey.exploit import IAgentOTPProvider
from infection_monkey.i_puppet import TargetHost
from infection_monkey.model import MONKEY_ARG
from infection_monkey.utils.commands import build_monkey_commandline_parameters, build_run_command


def build_ssh_command(
    agent_id: AgentID,
    target_host: TargetHost,
    servers: Sequence[str],
    current_depth: int,
    remote_agent_binary_destination_path: PurePath,
    otp_provider: IAgentOTPProvider,
) -> str:
    otp = otp_provider.get_otp()
    cmdline_arguments = build_monkey_commandline_parameters(
        parent=agent_id, servers=servers, depth=current_depth + 1
    )
    run_command = build_run_command(
        target_host=target_host,
        otp=otp,
        dst=remote_agent_binary_destination_path,
        args=[MONKEY_ARG, *cmdline_arguments],
    )

    cmdline = f"{run_command} > /dev/null 2>&1 &"

    return cmdline
